package me.numark;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import me.numark.model.ProfileResponse;
import me.numark.net.GsonRequest;
import me.numark.net.NetworkConstants;
import me.numark.net.NetworkHelper;
import me.numark.utils.Constants;
import me.numark.utils.Settings;


public class MessageIntervalActivity extends BaseActivity
        implements AdapterView.OnItemClickListener {

    private MessageIntervalListAdapter mAdapter;

    @InjectView(R.id.back)
    View back;

    @InjectView(R.id.title)
    TextView title;

    @InjectView(R.id.list)
    ListView mListView;

    private int mOldInterval;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_interval);
        ButterKnife.inject(this);
        mOldInterval = getIntent().getIntExtra(Constants.MESSAGE_INTERVAL_EXTRA, -1);
        initData();
        mListView = (ListView) findViewById(R.id.list);
        mAdapter = new MessageIntervalListAdapter();
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title.setText(R.string.new_message_interval);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Interval interval = mAdapter.getItem(position);
        setInterval(interval);
    }

    private class MessageIntervalListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mIntervalList.size();
        }

        @Override
        public Interval getItem(int position) {
            return mIntervalList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            if (view == null) {
                view = getLayoutInflater()
                        .inflate(R.layout.interval_list_item, parent, false);
            }
            Interval interval = getItem(position);
            TextView textView = (TextView) view.findViewById(R.id.text);
            textView.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                    interval.time == mOldInterval ?
                            R.drawable.multi_select_button_selected : 0, 0);
            textView.setText(interval.name);
            return view;
        }
    }

    private class Interval {

        String name;

        int time;

        public Interval(String name, int time) {
            this.name = name;
            this.time = time;
        }
    }

    private void initData() {
        mIntervalList.add(new Interval("随时", 0));
        mIntervalList.add(new Interval("30分钟", 30));
        mIntervalList.add(new Interval("2小时", 120));
        mIntervalList.add(new Interval("5小时", 600));
        mIntervalList.add(new Interval("24小时", 1440));
        mIntervalList.add(new Interval("取消提醒", -1));
    }

    private List<Interval> mIntervalList = new ArrayList<Interval>();

    private void setInterval(Interval interval) {
        String url = String.format(NetworkConstants.MESSAGE_INTERVAL_URL, Settings.getUid());
        Map<String, String> body = new HashMap<String, String>();
        body.put("interval", Integer.toString(interval.time));
        GsonRequest<ProfileResponse> request = new GsonRequest<ProfileResponse>(url,
                Request.Method.POST, ProfileResponse.class, true, body,
                new Response.Listener<ProfileResponse>() {
                    @Override
                    public void onResponse(ProfileResponse response) {
                        dismissProgress();
                        if (response.ret) {
                            Settings.saveProfile(response.data.toString());
                            Toast.makeText(MessageIntervalActivity.this,
                                    R.string.set_interval_success, Toast.LENGTH_SHORT);
                            finish();
                        } else {
                            Toast.makeText(MessageIntervalActivity.this, response.msg,
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                dismissProgress();
                Toast.makeText(MessageIntervalActivity.this, R.string.generic_network_error,
                        Toast.LENGTH_SHORT).show();
            }
        });
        showProgress();
        NetworkHelper.getInstance().addToRequestQueue(request);
        NetworkHelper.getInstance().getRequestQueue().start();
    }
}